package br.com.lh.ura.adapter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;

import br.com.lh.ura.database.FlexExtension;
import br.com.lh.ura.database.FlexSoundFile;
import br.com.lh.ura.database.UraXml;
import br.com.locaweb.pabx.core.models.Extension_;
import br.com.locaweb.pabx.core.models.SoundFile;
import br.com.locaweb.pabx.core.models.SoundList;
import br.com.locaweb.pabx.core.models.UraComponent;
import br.com.locaweb.pabx.core.models.WuraExtension;

public class UraAdapter {

    private static final long serialVersionUID = 1L;

    public static UraXml getUraXml(WuraExtension extension) throws CloneNotSupportedException {
	UraXml uraXml = new UraXml();
	uraXml.alias = extension.getAlias();
	uraXml.xmlString = extension.getDefinition();
	for (UraComponent uraView : extension.getUraComponentList()) {
	    UraComponent flexView = (UraComponent) uraView.clone();
	    uraXml.uraViewList.add(flexView);
	}
	uraXml.soundFileList = getFlexSoundList(extension.getCompany().getSoundLists());
	uraXml.availableExtension = getFlexAvailableExtension(extension.getCompany()
		.getExtensions_());
	uraXml.uraExtension = extension.getNumber();
	uraXml.uraId = extension.getId().toString();
	return uraXml;
    }

    public static List<FlexExtension> getFlexAvailableExtension(SortedSet<Extension_> extensions_) {
	List<FlexExtension> flexExtensions = new ArrayList<FlexExtension>();
	for (Extension_ e : extensions_) {
	    flexExtensions.add(new FlexExtension(e));
	}
	return flexExtensions;
    }

    public static List<FlexSoundFile> getFlexSoundList(Set<SoundList> soundLists) {
	Set<FlexSoundFile> flexSoundList = new HashSet<FlexSoundFile>();
	for (SoundList soundList : soundLists) {
	    for (SoundFile soundFile : soundList.getFiles()) {
		flexSoundList.add(new FlexSoundFile(soundFile));
	    }
	}
	return new ArrayList<FlexSoundFile>(flexSoundList);

    }
}
